/**
 * @author VernHe
 * @date 2021年06月24日 10:15
 */
public class Solution_0236 {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        return afterOrder(root,p,q);
    }

    private TreeNode afterOrder(TreeNode node, TreeNode p, TreeNode q) {
        if (node == null) {
            return null;
        }
        TreeNode left = afterOrder(node.left, p, q);
        TreeNode right = afterOrder(node.right, p, q);

        // 找到了就返回
        if (node.val == p.val || node.val == q.val) {
            System.out.println("当前根: " + node.val);
            if (left != null || right != null) {
            }
            return node;
        }
        // 没找到
        else if (null != left && null != right) {
            //在左右
            return node;
        }
        if (null != left) {
            return left;
        }
        if (null != right) {
            return right;
        }
        return null;
    }
}
